package com.stark.wechat.config;

import org.apache.ibatis.session.ExecutorType;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.core.io.ClassPathResource;

import com.stark.wechat.context.MultipleDataSource;

/**
 * MyBatis 配置。
 * @author mengbin
 * @since 1.1.0
 * @version 1.0.0, 2017/9/22
 */
@Configuration
public class MyBatisConfig {
	@Bean
	public SqlSessionFactoryBean sqlSessionFactory(MultipleDataSource multipleDataSource) {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(multipleDataSource);
		sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("myBatisConfig.xml"));
		return sqlSessionFactoryBean;
	}
	
	@Bean
	@Description("支持批量操作的 sqlSession")
	public SqlSessionTemplate sqlSession(MultipleDataSource multipleDataSource) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory(multipleDataSource).getObject(), ExecutorType.BATCH);
	}
	
	@Bean
	public MapperScannerConfigurer mapperScannerConfigurer() {
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		mapperScannerConfigurer.setBasePackage("com.stark.wechat.dao");
		mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
		return mapperScannerConfigurer;
	}
}
